package door.utils;

import app.nbdevices.utils.*;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SendCmd {




    public static void main(String[] args) throws Exception {


//        String url = "http://127.0.0.1:8081/test";
//        RestTemplate client = new RestTemplate();
//        HttpHeaders headers = new HttpHeaders();
////  请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
////  封装参数，千万不要替换为Map与HashMap，否则参数无法传递
//        MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
////  也支持中文
//        params.add("internetDevId", "用户名");
//        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
////  执行HTTP请求
//        ResponseEntity<Integer> response = client.exchange(url, HttpMethod.POST, requestEntity, Integer.class);
////  输出结果
//        System.out.println(response.getBody());

//        String internetDevId="dfdfdfdd";
//        Map<String,Object> params=new HashMap<>(1);
//        params.put("internetDevId",internetDevId);
//        Integer forObject = new RestTemplate().getForObject("http://127.0.0.1:8081/test", Integer.class, params);
////        HttpsUtil httpsUtil=new HttpsUtil();
////        httpsUtil.initSSLConfigForTwoWay();
////
////        doorOper("a4309ee9-9358-4671-a128-707d18f2a6aa","T00999",DoorCmdGenerator.doorOperator(1,0),httpsUtil);
//        System.out.println(forObject);
//        HttpsUtil httpsUtil=new HttpsUtil();
//     doorOper("6e3c5f6d-7c22-4569-a837-a12c29f77e31","T00802",DoorCmdGenerator.doorOperator(1,0),httpsUtil);
        System.out.println(DoorCmdGenerator.doorOperator(1,0));
    }
    public static void doorOper(String deviceId,String addrId,String cmdData,HttpsUtil httpsUtil)throws Exception{

        String baseAddrId= ConvertUtil.getBase64Codec().encodeAsString(addrId.getBytes());

        // Two-Way Authentication
      //  HttpsUtil httpsUtil=new HttpsUtil();
        httpsUtil.initSSLConfigForTwoWay();

        // Authentication.get token
        String accessToken = login(httpsUtil);

        String urlCreateDeviceCommand = Constant.CREATE_DEVICE_CMD;
        String appId = Constant.APPID;
        String callbackUrl=Constant.CALLBACK_BASE_URL;
       // String deviceId= "12f267e5-7041-41c7-8dcb-374f2dc39066";
        Integer expireTime = 0;
        Integer maxRetransmit = 3;

        String serviceId="doorOper";
        String method="doorCmd";
        String c="{\"addrId\":\""+baseAddrId+"\",\"cmdData\":\""+cmdData+"\"}";
//        String cmd="{\"addrId\":\"VDAwMDAx\",\"cmdData\":\"AQBV\"}";
//        System.out.println(c+" "+cmd);
        ObjectNode paras = JsonUtil.convertObject2ObjectNode(c);

        Map<String, Object> paramCommand = new HashMap<>();
        paramCommand.put("serviceId", serviceId);
        paramCommand.put("method", method);
        paramCommand.put("paras", paras);

        Map<String, Object> paramCreateDeviceCommand = new HashMap<>();
        paramCreateDeviceCommand.put("deviceId", deviceId);
        paramCreateDeviceCommand.put("command", paramCommand);
        paramCreateDeviceCommand.put("callbackUrl", callbackUrl);
        paramCreateDeviceCommand.put("expireTime", expireTime);
        paramCreateDeviceCommand.put("maxRetransmit", maxRetransmit);

        String jsonRequest = JsonUtil.jsonObj2Sting(paramCreateDeviceCommand);

        System.out.println(jsonRequest);

        Map<String, String> header = new HashMap<>();
        header.put(Constant.HEADER_APP_KEY, appId);
        header.put(Constant.HEADER_APP_AUTH, "Bearer" + " " + accessToken);

        HttpResponse responseCreateDeviceCommand = httpsUtil.doPostJson(urlCreateDeviceCommand, header, jsonRequest);

        String responseBody = httpsUtil.getHttpResponseBody(responseCreateDeviceCommand);

        System.out.println("CreateDeviceCommand, response content:");
        System.out.println(responseCreateDeviceCommand.getStatusLine());
        System.out.println(responseBody);
        System.out.println();
    }
    public static String login(HttpsUtil httpsUtil) throws Exception {

        String appId = Constant.APPID;
        String secret = Constant.SECRET;
        String urlLogin = Constant.APP_AUTH;

        Map<String, String> paramLogin = new HashMap<>();
        paramLogin.put("appId", appId);
        paramLogin.put("secret", secret);

        StreamClosedHttpResponse responseLogin = httpsUtil.doPostFormUrlEncodedGetStatusLine(urlLogin, paramLogin);

        System.out.println("app auth success,return accessToken:");
        System.out.println(responseLogin.getStatusLine());
        System.out.println(responseLogin.getContent());
        System.out.println();

        Map<String, String> data = new HashMap<>();
        data = JsonUtil.jsonString2SimpleObj(responseLogin.getContent(), data.getClass());
        return data.get("accessToken");
    }
}
